package com.xiaxiang.xiawan.Effect;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.donggua.aichebao.photo.DGPhotoBase;
import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.VideoPlayer.ARVideoView;
import com.xiaxiang.xiawan.video.VideoInfoListener;
import com.xiaxiang.xiawan.video.XXVideoBase;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//import com.xx.xiawan.videoplayer.XXVideoPlayer;

/**
 * Created by Administrator on 2017-10-26.
 */

class ARMediaOperator implements VideoInfoListener,View.OnClickListener {
    private View mMainView;
    private Activity mAct;
    private XXVideoBase mVideoPlayer;
    private ImageView mImgStart;
    private boolean mIsStartPlayer = false;
    private boolean mIsVideoStart = false;
    private boolean isFirstStart = false;
    private boolean mIsPause = false;
    private String mVideoPath = null;
    ARMediaOperator(View mainView,Activity activity){
        mMainView = mainView;
        mAct = activity;
        mImgStart = (ImageView)mainView.findViewById(R.id.imgStart);
        ARVideoView videoView = (ARVideoView)mainView.findViewById(R.id.viVideoPlayer);
        mImgStart.setOnClickListener(this);
        mVideoPlayer = getVideoPlayer("com.xx.xiawan.videoplayer.XXVideoPlayer",mAct);
        if(mVideoPlayer != null){
            mVideoPlayer.setVideoView(videoView);
            mVideoPlayer.setOnInfoListener(this);
            // mVideoPlayer.setVideoPath("file:///android_asset/test.mp4");
            // mVideoPlayer.setVideoPath("/mnt/sdcard/mp4.mp4");
          //  mVideoPlayer.setVideoPath("/mnt/emmc/test.mp4");
        }

    }

    @Override
    public void onPrepared() {

    }

    static String TAG = "mei";
    @Override
    public void onCompletion() {
        mIsVideoStart = false;
        mIsStartPlayer = false;
        isFirstStart = false;
        mIsPause = false;
        Log.e(TAG," onCompletion");
        mImgStart.setImageResource(R.mipmap.show_start);
    }

    @Override
    public void mediaInfoBufferingStart(int extra) {
        Log.e(TAG," mediaInfoBufferingStart");
        mIsVideoStart = false;
        mIsStartPlayer = false;
    }

    @Override
    public void mediaInfoBufferingEnd(int extra) {
        Log.e(TAG," mediaInfoBufferingEnd");
        mIsVideoStart = true;
        mIsStartPlayer = true;
        isFirstStart = true;
    }

    @Override
    public void mediaInfoRenderingStart(int extra) {
        Log.e(TAG," mediaInfoRenderingStart");
        mIsVideoStart = true;
        mIsStartPlayer = true;
        isFirstStart = true;
    }

    @Override
    public void mediaInfoBufferingBytesUpdate(int extra) {
        Log.e(TAG," mediaInfoBufferingBytesUpdate");
    }

    @Override
    public void doOther(int extra) {

    }

    public void onClick(View view) {
        if(mVideoPath == null || mVideoPath.length() == 0){
            return;
        }
        int id = view.getId();
        switch (id){
            case R.id.imgStart:
                if(!mIsStartPlayer){
                    if(isFirstStart){
                   //     mIsStartPlayer = true;
                    }
                    mIsStartPlayer = true;
                    mIsVideoStart = true;
                    mIsPause = false;
                    mVideoPlayer.start();
                    mImgStart.setImageResource(R.mipmap.show_stop);

                }else{
                    mIsStartPlayer = false;
                    mVideoPlayer.pause();
                    mImgStart.setImageResource(R.mipmap.show_start);
                    mIsPause = true;
                }
                break;
        }
    }

    public void onPause() {
        Log.e(TAG," mIsStartPlayer is "+mIsStartPlayer+" mIsVideoStart is "+mIsVideoStart+" mIsPause is "+mIsPause);
        if(mIsStartPlayer && mIsVideoStart && !mIsPause){
            mIsVideoStart = false;
            mVideoPlayer.pause();
            mImgStart.setImageResource(R.mipmap.show_start);
        }

    }

    public void onResume() {
        if(mIsStartPlayer && !mIsVideoStart && !mIsPause){
            mIsVideoStart = true;
            mVideoPlayer.start();
            mImgStart.setImageResource(R.mipmap.show_stop);
        }

    }

    public void onDestroy() {
        if(mVideoPlayer != null){
            mVideoPlayer.stopPlayback();
        }
        mIsStartPlayer = false;
        mIsVideoStart = false;
        mIsPause = false;
    }

    public void setVideoPath(String path){
        mVideoPath = path;
        if(mVideoPlayer != null) {
            mVideoPlayer.setVideoPath(path);
        }
    }

    public void startVideoPlayer()
    {
        if(mVideoPlayer != null){
            mVideoPlayer.start();
            mImgStart.setImageResource(R.mipmap.show_stop);
        }
    }

    public void pauseVideoPlayer(){
        if(mVideoPlayer != null) {
            mVideoPlayer.pause();
            mImgStart.setImageResource(R.mipmap.show_start);
        }
    }

    public static XXVideoBase getVideoPlayer(String clsName , Activity activity)  {
        Object object = null;
        try {
            Class mLoadClass = Class.forName(clsName);
            Constructor constructor = mLoadClass.getConstructor(Activity.class);
            object = constructor.newInstance(activity);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return (XXVideoBase)object;
    }
}
